/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.TheCaptainsLogs
{
	[CustomBehaviorFileName(@"SpecificQuests\Kalimdor\Azshara\14134-TheCaptainsLogs")]
	public class TheCaptainsLogs : CustomForcedBehavior
	{
		public TheCaptainsLogs(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 14134;//GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: The Captain's Logs");
			}
		}
		public int QuestId { get; set; }
		//private Composite _behaviorTreeHook_LootMain;
		private bool _isBehaviorDone;
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		static public bool InVehicle { get { return Lua.GetReturnVal<int>("if IsPossessBarVisible() or UnitInVehicle('player') or not(GetBonusBarOffset()==0) then return 1 else return 0 end", 0) == 1; } }
		public bool Local1 = true;
		public bool Local2 = true;
		public bool Local3 = true;
		public bool Local4 = true;
		public WoWPoint Location1 = new WoWPoint(2655.93, -4869.13, 126.23);
		public WoWPoint Location2 = new WoWPoint(2659.35, -4932.69, 121.98);
		public WoWPoint Location3 = new WoWPoint(2610.88, -4987.64, 122.68);
		public WoWPoint Location4 = new WoWPoint(2615.28, -5050.55, 120.92);
		
		public static void BPdiag(string message, params object[] args) { Logging.WriteDiagnostic(Colors.LawnGreen, "[QB DIAG TBM: " + message, args); }
		
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				PlayerQuest Quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}

		public WoWGameObject LogPile
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWGameObject>().Where(
					u => u.Entry == 195361 && u.Distance < 150 && !Blacklist.Contains(u.Guid, BlacklistFlags.Interact))
					.OrderBy(u => u.Distance).FirstOrDefault();
			}
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite LogGrab
		{
			get
			{
				return new PrioritySelector(
					new Decorator(context => LogPile != null,
						new PrioritySelector(
							new Action(context =>
							{
								BPdiag("Detected LogPile (dist: {0:F1})!", LogPile.Distance);
								return RunStatus.Failure;
							}),
							new Decorator(context => !LogPile.WithinInteractRange,
								new Action(context => { Navigator.MoveTo(LogPile.Location); })),
							new Decorator(context => Me.CharmedUnit.IsMoving,
								new Action(context => { WoWMovement.MoveStop(); })),
							new Action(context => { Lua.DoString("CastPetAction(1)"); }),
							new Decorator(context => !Me.IsCasting,
								new Action(context => { Blacklist.Add(LogPile.Guid, BlacklistFlags.Interact, TimeSpan.FromSeconds(3 * 60)); }))
					)),
					
					new Decorator(context => LogPile == null,
						new Sequence(
							new Action(context => BPdiag("Detected No LogPile!")),
							new Decorator(context => Local1,
								new Sequence(
									new Action(context => BPdiag("Move to Local1!")),
									new Action(context => TreeRoot.StatusText = "Moving to 1st location"),
									new Action(context => Navigator.MoveTo(Location1)),
									new Action(context => Local1 = false),
									new ActionAlwaysSucceed())),
							new Decorator(context => Local2,
								new Sequence(
									new Action(context => BPdiag("Move to Local2!")),
									new Action(context => TreeRoot.StatusText = "Moving to 2nd location"),
									new Action(context => Navigator.MoveTo(Location2)),
									new Action(context => Local2 = false),
									new ActionAlwaysSucceed())),
							new Decorator(context => Local3,
								new Sequence(
									new Action(context => BPdiag("Move to Local3!")),
									new Action(context => TreeRoot.StatusText = "Moving to 3rd location"),
									new Action(context => Navigator.MoveTo(Location3)),
									new Action(context => Local3 = false),
									new ActionAlwaysSucceed())),
							new Decorator(context => Local4,
								new Sequence(
									new Action(context => BPdiag("Move to Local4!")),
									new Action(context => TreeRoot.StatusText = "Moving to 4th location"),
									new Action(context => Navigator.MoveTo(Location4)),
									new Action(context => Local4 = false),
									new ActionAlwaysSucceed())),
							new Decorator(context => !Local1 && !Local2 && !Local3 && Local4,
								new Sequence(
									new Action(context => BPdiag("All locals false, reset them!")),
									new Action(context => Local1 = true),
									new Action(context => Local2 = true),
									new Action(context => Local3 = true),
									new Action(context => Local4 = true),
									new ActionAlwaysSucceed())))));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => !InVehicle,
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet, LogGrab, new ActionAlwaysSucceed())));
		}
	}
}